//
//  RoomDetailsHandler.m
//  mUBB
//
//  Created by Cosmin Stirbu on 1/25/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "RoomDetailsHandler.h"
#import "RoomsProtocol.h"



@implementation RoomDetailsHandler

@synthesize room;

- (id)initWithDelegate:(id)_delegate room:(Room *)_room ticket:(Ticket *)_ticket{
    self = [super initWithDelegate:_delegate ticket:_ticket];
    [self setRoom:_room];
    return self;
}

- (void)handleSuccess:(WSResponse *)response {
    NSDictionary *roomDictionary = [response.data objectFromJSONData];
    
    // Room Id
    room.roomId = [[roomDictionary getJSONObjectForKey:ROOM_ID_TAG] integerValue];
    // Room Name
    room.name = [roomDictionary getJSONObjectForKey:ROOM_NAME_TAG];
    // Self URL
    room.selfUrl = [roomDictionary getJSONObjectForKey:SELF_URL_TAG];
    
    // Has Projector
    room.hasProjector = [[roomDictionary getJSONObjectForKey:HAS_PROJECTOR_TAG] boolValue];
    
    // Number of places
    room.numberOfPlaces = [[roomDictionary getJSONObjectForKey:NUMBER_OF_PLACES_TAG] integerValue];
    
    // Bookings URI
    room.bookingsURL = [roomDictionary getJSONObjectForKey:BOOKINGS_URL_TAG];
    
    // Bulding
    NSDictionary *buildingDictionary = [roomDictionary getJSONObjectForKey:BUILDING_TAG];
    
    if (room.building == nil) {
        room.building = [Building new];
    }
    // Building Id
    room.building.buildingId = [[buildingDictionary getJSONObjectForKey:BUILDING_ID_TAG] integerValue];
    
    // Building name
    room.building.name = [buildingDictionary getJSONObjectForKey:BUILDING_NAME_TAG];
    
    // Building street
    room.building.street = [buildingDictionary getJSONObjectForKey:BUILDING_STREET_TAG];
    
    // Building Zip Code
    room.building.zipCode = [buildingDictionary getJSONObjectForKey:BUILDING_ZIP_CODE_TAG];
    
    // Building City
    room.building.city = [buildingDictionary getJSONObjectForKey:BUILDING_CITY_TAG];
    
    // Latitude & Longitude
    CGFloat latitutde = [[buildingDictionary getJSONObjectForKey:BUILDING_LATITUDE_TAG] floatValue];
    CGFloat longitude = [[buildingDictionary getJSONObjectForKey:BUILDING_LONGITUDE_TAG] floatValue];
    
    room.building.coordinates = CLLocationCoordinate2DMake(latitutde, longitude);
    
    // Bookings URL
    room.bookingsURL = [roomDictionary getJSONObjectForKey:BOOKINGS_URL_TAG];
    
    [self.delegate didReceiveRoomDetailsWithSuccess:room forTicket:self.ticket];
}

- (void)handleError:(WSError *)error {
    [self.delegate didReceiveRoomDetailsWithFailure:error forTicket:self.ticket];
}
@end
